<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="mini" inline>
      <el-form-item prop="courseName">
        <el-input v-model.trim="queryParams.courseName" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" :icon="isExpand ? 'el-icon-arrow-down' : 'el-icon-arrow-right'" @click="handleExpandDebounce">全部{{ isExpand ? '折叠' : '展开' }}</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="$refs.EditRdcCourse.open({}, { rdcCourseList })" v-hasPermi="['cq:rdcCourse:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table class="g-table-tree-expand-align" ref="table" v-loading="loading" :data="list" size="small" row-key="courseId" :default-expand-all="isExpand" height="calc(100vh - 150px)">
      <el-table-column label="编码" prop="code" show-overflow-tooltip min-width="150" class-name="expand-column" />
      <el-table-column label="名称" prop="courseName" show-overflow-tooltip min-width="150" />
      <el-table-column label="类型" prop="courseType" align="center" width="80">
        <span slot-scope="{ row }">{{ row.courseType | courseTypeFilter }}</span>
      </el-table-column>
      <el-table-column label="是否启用" align="center" min-width="80">
        <el-tag slot-scope="{ row }" size="small" :type="row.isEnable ? 'primary' : 'info'">{{ row.isEnable ? '启用' : '禁用' }}</el-tag>
      </el-table-column>
      <el-table-column label="排序" prop="seq" align="center" width="50" />
      <el-table-column label="修改人" prop="updator" align="center" show-overflow-tooltip min-width="90" />
      <el-table-column label="最后修改时间" align="center" show-overflow-tooltip min-width="120">
        <span slot-scope="{ row }">{{ row.updateTime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" fixed="right">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditRdcCourse.open(row, { rdcCourseList })" v-hasPermi="['cq:rdcCourse:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-hasPermi="['cq:rdcCourse:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="$refs.EditRdcCourse.open({ parentId: row.courseId }, { rdcCourseList })" v-hasPermi="['cq:rdcCourse:add']">新增</el-button>
        </template>
      </el-table-column>
    </el-table>

    <EditRdcCourse ref="EditRdcCourse" v-model="showEditRdcCourse" @ok="getList" />
  </div>
</template>
<script>
// 工程质量-其他抽检流程
import { debounce, flatList2tree } from '@/utils'
import { cqRdcCourseTypes } from '@/utils/baseData'
import { listRdcCourse, delRdcCourse } from '@/api/qualityManage/cq'
import EditRdcCourse from './components/EditRdcCourse'
export default {
  name: 'RdcCourse',
  components: { EditRdcCourse },
  filters: {
    courseTypeFilter(val) {
      const item = cqRdcCourseTypes.find(i => i.value === val)
      return item?.label || val
    }
  },
  data() {
    return {
      isExpand: true,
      loading: false,
      list: [],
      queryParams: {
        courseName: ''
      },
      showEditRdcCourse: false,
      rdcCourseList: [] // 抽检流程原数据
    }
  },
  created() {
    this.handleExpandDebounce = debounce(this.handleExpand)
    this.getList()
  },
  methods: {
    // 递归切换展开状态
    setChildren(children, expanded) {
      children.map(row => {
        this.$refs.table.toggleRowExpansion(row, expanded)
        if (row.children) this.setChildren(row.children, expanded)
      })
    },
    // 展开/折叠
    handleExpand() {
      this.isExpand = !this.isExpand
      this.setChildren(this.list, this.isExpand)
    },
    async getList() {
      this.loading = true
      const res = await listRdcCourse(this.queryParams).catch(() => false)
      this.loading = false
      if (res === false) return

      let data = res.data || []
      this.rdcCourseList = JSON.parse(JSON.stringify(data))
      data.map(i => {
        i.removeLoading = false
      })
      data = flatList2tree(data, { id: 'courseId' })
      this.list = data
      this.isExpand = true
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除：${item.courseName}？`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delRdcCourse(item.courseId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
